package fg.animation.easing;

import fg.math.Easing;

/**
 * Simple easing animation for a single value.
 * 
 * @author robin
 * 
 */
public class ValueEasingAnimation extends EasingAnimation {

	private float start;
	private float end;
	public float value;
	private Listener listener;

	public ValueEasingAnimation(int duration, float start, float end, Easing easing) {
		super(duration, easing);
		this.start = start;
		this.end = end;
		this.value = start;
	}

	public ValueEasingAnimation(int duration, float start, float end) {
		super(duration, Easing.LINEAR);
		this.start = start;
		this.end = end;
		this.value = start;
	}

	/**
     * @return the listener
     */
    public Listener getListener() {
    	return listener;
    }

	/**
     * @param listener the listener to set
     */
    public void setListener(Listener listener) {
    	this.listener = listener;
    }

	/**
	 * The "CallBack" value. Use this value to update your animated value.
	 * 
	 * @return animated value
	 */
	public float getValue() {
		return value;
	}

	public void animate(int time) {
		float change = end - start;
		float duration = (float) getDuration();
		value = easing.ease(time, start, change, duration);
		if (null != listener) {
			listener.onEvent(value);
		}
	}
	
	public void reset() {
		value = start;
	}

	/**
	 * @return the start
	 */
	public float getStart() {
		return start;
	}

	/**
	 * @param start
	 *            the start to set
	 */
	public void setStart(float start) {
		this.start = start;
	}

	/**
	 * @return the end
	 */
	public float getEnd() {
		return end;
	}

	/**
	 * @param end
	 *            the end to set
	 */
	public void setEnd(float end) {
		this.end = end;
	}
	
	public static interface Listener {
		
		public void onEvent(float value);
		
	}

}
